<template>
  <div>
    <h1>{{ id ? "编辑" : "新建" }}管理员</h1>
    <el-form label-width="80px" @submit.native.prevent>
      <el-form-item label="用户名">
        <el-input v-model="model.username" placeholder="用户名"></el-input>
      </el-form-item>

      <el-form-item label="密码">
        <el-input v-model="model.password" placeholder="请输入密码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="save()">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      model: {
        name: "",
        password: ""
      },
      parents: []
    };
  },
  props: {
    id: { type: String }
  },

  methods: {
    save() {
      if (this.id) {
        this.$http.put(`/rest/admin_user/${this.id}`, this.model).then(() => {
          this.$message({
            message: "修改成功",
            type: "success"
          });
        });
        this.$router.push("/admin_user/list");
      } else {
        this.$http.post("/rest/admin_user", this.model).then(() => {
          this.$message({
            message: "保存成功",
            type: "success"
          });
        });
        this.$router.push("/admin_user/list");
      }
    },

    fetch() {
      this.$http.get(`/rest/admin_user/${this.id}`).then(res => {
        // this.model=res.data
        this.model = res.data;
      });
    }
  },

  created() {
    this.id && this.fetch();
  }
};
</script>
